package ren.steve.common.result;

import lombok.Data;

import java.util.Date;

/**
 * @Author: LiuGuoJia
 * @Description:
 * @Date: Created in 2024/1/18 17:28
 */


@Data
public class Result<T> {

  private Integer code;
  private String message;
  private T data;

  private Result () {}

  // 返回数据
  protected static <T> Result<T> build(T data) {
    Result<T> result = new Result<T>();
    if (data != null) {
      result.setData(data);
    }
    return result;
  }

  public static <T> Result<T> build(T body, Integer code, String message) {
    Result<T> result = build(body);
    result.setCode(code);
    result.setMessage(message);
    return result;
  }

  public static <T> Result<T> build(T body, HttpStatus httpStatus) {
    Result<T> result = build(body);
    result.setCode(httpStatus.getCode());
    result.setMessage(httpStatus.getMessage());
    return result;
  }

  public static<T> Result<T> ok(HttpStatus httpStatus){
    return Result.ok(null, httpStatus);
  }

  /**
   * 操作成功
   * @param data  baseCategory1List
   * @param <T>
   * @return
   */
  public static<T> Result<T> ok(T data, HttpStatus httpStatus){
    return build(data, httpStatus);
  }

  public static<T> Result<T> fail(){
    return Result.fail(null);
  }

  /**
   * 操作失败
   * @param data
   * @param <T>
   * @return
   */
  public static<T> Result<T> fail(T data){
    Result<T> result = build(data);
    return build(data, HttpStatus.EXPECTATION_FAILED);
  }

  public Result<T> message(String msg){
    this.setMessage(msg);
    return this;
  }

  public Result<T> code(Integer code){
    this.setCode(code);
    return this;
  }

}
